<?php
/**
 * @package JoiCaddy
 * @copyright Авторские права (C) 2008-2010 Joostina team. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * JoiCaddy - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * JoiCaddy основан на разработках SimpleCaddy от Henk von Pickartz
 */

defined('_VALID_MOS') or die();

function com_install() {

  $mainframe = &mosMainFrame::getInstance();
  $db = &database::getInstance();

  //РАСПАКОВКА АРХИВОВ  
  //Файлы фронта
  $zipfile = JPATH_BASE.DS.'components'.DS.'com_caddy'.DS.'front.zip';
  $unzip_dir = JPATH_BASE.DS.'components'.DS.'com_caddy';
  if(!joi_unzip($zipfile, $unzip_dir)) return false;
  joi_chmod ($unzip_dir);

  //Файлы админки
  $zipfile = JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_caddy'.DS.'admin.zip';
  $unzip_dir = JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_caddy';
  if(!joi_unzip($zipfile, $unzip_dir)) return false;
  joi_chmod ($unzip_dir);

  $query="show columns from #__sc_orders";
	$db->setQuery($query);
	$lstcf=$db->loadObjectList();
	$customfield=false;
	$ipaddress=false;
	$archive=false;
	foreach ($lstcf as $f) {
		if ($f->Field=='customfields') {
			$customfield=true;
			if ($f->Type=="varchar(255)") {
				// change the field type
				$query="ALTER TABLE `#__sc_orders` CHANGE `customfields` `customfields` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL;";
				$db->setQuery($query);
				$db->query();
				$customfield=true;
				echo "Changing customfields type to TEXT; <br />";
			}
		}
		if ($f->Field=='ipaddress') {
			$ipaddress=true;
		}
		if ($f->Field=='archive') {
			$archive=true;
		}
		if ($f->Field=='shipCost') {
			$shipcost=true;
		}
		if ($f->Field=='shipRegion') {
			$shipregion=true;
		}
	}
	if (!$archive) {
		// add the customfield
		$query="ALTER TABLE `#__sc_orders` ADD  `archive` INT(11) NULL;";
		$db->setQuery($query);
		$db->query();
		echo "Adding archive to database";
	}
	if (!$customfield) {
		// add the customfield
		$query="ALTER TABLE `#__sc_orders` ADD  `customfields` TEXT NULL;";
		$db->setQuery($query);
		$db->query();
		echo "Adding customfields to database";
	}
	if (!$ipaddress) {
		// add the customfield
		$query="ALTER TABLE `#__sc_orders` ADD  `ipaddress` varchar(32) NULL;";
		$db->setQuery($query);
		$db->query();
		echo "Adding ipaddress to database";
	}
	if (!$shipcost) {
		// add the customfield
		$query="ALTER TABLE `#__sc_orders` ADD  `shipCost` varchar(10) NULL;";
		$db->setQuery($query);
		$db->query();
		echo "Adding shipping cost to database";
	}
	if (!$shipregion) {
		// add the customfield
		$query="ALTER TABLE `#__sc_orders` ADD  `shipRegion` varchar(255) NULL;";
		$db->setQuery($query);
		$db->query();
		echo "Adding shipping region to database";
	}

	$query="show columns from #__sc_fields";
	$db->setQuery($query);
	$lstcf=$db->loadObjectList();
	$fieldcontents=false;
	foreach ($lstcf as $f) {
		if ($f->Field=='fieldcontents') {
			$fieldcontents=true;
		}
	}
	if (!$fieldcontents) {
		// add the customfield
		$query="ALTER TABLE `#__sc_fields` ADD  `fieldcontents` TEXT NULL;";
		$db->setQuery($query);
		$db->query();
		echo "Adding fieldcontnts to database";
	}

	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(1, 'name', 'Имя', 'text', 60, 'inputbox', 0, 0, 1, 'checkfilled', '');";
	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(2, 'address', 'Адрес', 'textarea', 0, 'inputbox', 1, 3, 1, 'checkfilled', '');";
	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(3, 'codepostal', 'Индекс', 'text', 10, 'inputbox', 0, 4, 1, 'checkfilled', '');";
	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(10, 'deliverybefore', 'Доставить до', 'date', 0, 'inputbox', 1, 99, 1, 'checkfilled', '');";
	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(5, 'city', 'Город', 'text', 40, 'inputbox', 0, 5, 1, 'checkfilled', '');";
	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(6, 'telephone', 'Телефон', 'text', 40, 'inputbox', 0, 15, 1, 'checkfilled', '');";
	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(7, 'email', 'Email', 'text', 40, 'inputbox', 0, 20, 1, 'checkfilled', '');";
	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(8, 'dropdown', 'Выпадающий список', 'dropdown', 0, 'inputbox', 1, 0, 1, 'checkfilled', 'een;twee;drie;vier;vijf');";
	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(11, 'div1', 'Расширенная информация', 'divider', 0, 'divider', 0, 10, 1, 'checkfilled', '');";
	$q[]="INSERT IGNORE INTO `#__sc_fields` VALUES(12, 'coupon', 'Введите купон (если есть)', 'voucher', 0, 'inputbox', 0, 0, 1, 'checkfilled', '');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(1, 'currency', 'CURRENCY_SYMBOL', 'CAD', 0, 'text', '', 0, 0, 'FINANCE');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(2, 'show_emptycart', 'SHOW_EMPTY_CART_BUTTON', '1', 0, 'yesno', '', 0, 0, 'FRONTEND_DISPLAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(3, 'thousand_sep', 'THOUSANDS_SEPARATOR', ',', 0, 'text', '', 0, 0, 'FRONTEND_DISPLAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(4, 'decimal_sep', 'DECIMAL_SEPARATOR', '.', 0, 'text', '', 0, 0, 'FRONTEND_DISPLAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(5, 'decimals', 'NUMBER_OF_DECIMALS', '2', 0, 'text', '', 0, 0, 'FRONTEND_DISPLAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(6, 'remove_button', 'SHOW_REMOVE_BUTTON', '1', 0, 'yesno', '', 0, 0, 'FRONTEND_DISPLAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(7, 'email_customer', 'SEND_CONFIRMATION_EMAIL_TO_CUSTOMER', '1', 0, 'yesno', '', 0, 0, 'COMMUNICATION');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(8, 'email_copies', 'SEND_CONFIRMATION_EMAIL_COPIES_TO', 'me@mysite.com\r\nyou@mysite.com', 0, 'textarea', '', 40, 10, 'COMMUNICATION');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(9, 'curralign', 'CURRENCY_SYMBOL_POSITION', '0', 0, 'list', 'BEFORE_AMOUNT:1\r\nAFTER_AMOUNT:0', 0, 0, 'FRONTEND_DISPLAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(10, 'ostatus', 'ORDER_STATUSES_ONE_PER_LINE', 'New\r\nReviewed\r\nReview\r\nCancelled\r\nTreated\r\nArchive', 0, 'textarea', '', 20, 10, 'ORDER_STATUSES');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(11, 'taxrate', 'TAX_RATE_TO_APPLY', '0', 0, 'text', '', 0, 0, 'FINANCE');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(12, 'emailhtml', 'EMAIL_IS_IN_HTML_FORMAT', '0', 0, 'yesno', '', 0, 0, 'COMMUNICATION');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(13, 'dateformat', 'DATE_FORMAT', 'd-m-y', 0, 'text', '', 0, 0, 'FRONTEND_DISPLAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(14, 'timeformat', 'TIME_FORMAT', 'h:i:s', 0, 'text', '', 0, 0, 'FRONTEND_DISPLAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(15, 'reselleremail', 'RESELLER_EMAIL_FOR_PAYPAL', 'change@me.com', 0, 'text', '', 40, 0, 'PAYPAL');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(16, 'usepaypal', 'USE_PAYPAL_FOR_CHECKOUT', '1', 0, 'yesno', '', 0, 0, 'PAYPAL');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(17, 'paypalcurrency', 'PAYPAL_CURRENCY', 'CAD', 0, 'list', 'Australian Dollars (AUD):AUD \r\nCanadian Dollars (CAD):CAD \r\nEuros (EUR):EUR \r\nPounds Sterling (GBP):GBP \r\nYen (YEN):JPY \r\nU.S. Dollars (USD):USD \r\nNew Zealand Dollar (NZD):NZD \r\nSwiss Franc:CHF \r\nHong Kong Dollar (HKD):HKD \r\nSingapore Dollar (SGD):SGD \r\nSwedish Krona:SEK \r\nDanish Krone:DKK \r\nPolish Zloty:PLN \r\nNorwegian Krone:NOK \r\nHungarian Forint:HUF \r\nCzech Koruna:CZK \r\nMexican Pesos:MXN \r\nSouth African Rand:R', 0, 0, 'PAYPAL');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(18, 'usecidasemail', 'USE_CONTENT_AS_EMAIL', '0', 0, 'yesno', '', 0, 0, 'COMMUNICATION');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(19, 'usestdproduct', 'USE_THE_SYSTEMATIC_PRODUCT', '0', 0, 'yesno', '', 10, 0, 'FINANCE');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(20, 'emailcid', 'CONFIRMATION_CONTENT_ID', '0', 0, 'text', '', 3, 0, 'COMMUNICATION');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(21, 'cart_fee_product', 'SYSTEMATIC_PRODUCT_CODE_TO_ADD_TO_CART', 'extracost', 0, 'text', '', 10, 0, 'FINANCE');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(22, 'prodcats', 'PRODUCT_CATEGORIES_ONE_PER_LINE', 'Food\r\nDrinks\r\nPlants\r\nChemistry\r\nNon-Food', 0, 'textarea', '', 40, 5, 'PRODUCT_CATEGORIES');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(23, 'ppreturnsuccess', 'PAYPAL_SUCCESS_RETURN_URL', '', 0, 'text', '', 80, 0, 'PAYPAL');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(24, 'ppreturncancel', 'PAYPAL_FAILURE_CANCEL_RETURN_URL', '', 0, 'text', '', 80, 0, 'PAYPAL');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(25, 'pgclassname', 'PAYMENT_GATEWAY_CLASSNAME', 'scpaypal', 0, 'text', '', 0, 0, 'PAYMENT_GATEWAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(26, 'mailengine', 'MAIL_ENGINE', 'joomla', 0, 'list', 'Joomla:joomla\r\nAlternative:alternative', 0, 0, 'COMMUNICATION');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(27, 'shippingenabled', 'IF_ENABLED_SHIPPING_ADDON_WILL_BE_USED_TO_CALCULATE_SHIPPING_COST', '0', 0, 'yesno', '', 0, 0, 'SHIPPING');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(28, 'checkminqty', 'CHECK_MINIMUM_QUANTITY_IN_DB', '0', 0, 'yesno', '', 0, 0, 'CHECKING_OUT');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(29, 'mincheckout', 'MINIMUM_AMOUNT_FOR_CHECKOUT', '0', 0, 'text', '', 0, 0, 'CHECKING_OUT');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(30, 'usevouchers', 'USE_THE_VOUCHERS', '0', 0, 'yesno', '', 0, 0, 'CHECKING_OUT');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(31, 'pretextid', 'CONTENT_ID_ABOVE_CART_DISPLAY', '0', 0, 'text', '', 0, 0, 'FRONTEND_DISPLAY');";
	$q[]="INSERT IGNORE INTO `#__sc_config` VALUES(32, 'posttextid', 'CONTENT_ID_BELOW_CART_DISPLAY', '0', 0, 'text', '', 0, 0, 'FRONTEND_DISPLAY');";
	
	foreach ($q as $key=>$query) {
		$db->setquery($query);
		$db->query();
	}

	$query="show columns from #__sc_products";
	$db->setQuery($query);
	$lstcf=$db->loadObjectList();
	$shippoints=false;
	foreach ($lstcf as $f) {
		if ($f->Field=='shippoints') {
			$shippoints=true;
		}
	}
	if (!$shippoints) {
		// add the customfield
		$query="ALTER TABLE `#__sc_products` ADD  `shippoints` INT(11);";
		$db->setQuery($query);
		$db->query();
		echo "Adding Shipping points to database";
	}

}//function com_install


function joi_chmod ($dir) {
  $filemode = 0644;
  $dirmode  = 0755;
  mosChmodRecursive($dir, $filemode, $dirmode);
  return true;
}

function joi_unzip($zip, $unzip_dir) {
  $mainframe = &mosMainFrame::getInstance();

  require_once (JPATH_BASE.'/administrator/includes/pcl/pclzip.lib.php');
  require_once (JPATH_BASE.'/administrator/includes/pcl/pclerror.lib.php');

  $zipfile = new PclZip($zip);

  $ret = $zipfile->extract(PCLZIP_OPT_PATH, $unzip_dir);
  if ($ret == 0) {
          echo "Неисправимая ошибка: " . $zipfile->errorName(true).'<br />';
          return false;
  }
  unlink($zip);
  return true;
}
?>